/**
* This file is part of OntoCompoFramework
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package ontocompo.ui.positioning;

import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.dnd.DropTarget;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JPanel;

public class GridRelativePane extends JPanel {
	
	private boolean uniqueComponent;
	
	private JComponent centeredComponent;
	
	private GridRelativePane topleft;
	private GridRelativePane top;
	private GridRelativePane topright;
	private GridRelativePane left;
	private GridRelativePane right;
	private GridRelativePane bottomleft;
	private GridRelativePane bottom;
	private GridRelativePane bottomright;
	
//	private DropTarget dt;
	
	public GridRelativePane(JComponent component) {
		this.uniqueComponent = true;
		this.centeredComponent = component;
//		this.dt = dt;
		
		this.setLayout(new GridBagLayout());
		
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		this.add(component, c);
		
		c.gridx = 0;
		c.gridy = 0;
		this.add(new RelativePosition(Position.TOPLEFT), c);
		
		c.gridx = 1;
		c.gridy = 0;
		this.add(new RelativePosition(Position.TOP), c);
		
		c.gridx = 2;
		c.gridy = 0;
		this.add(new RelativePosition(Position.TOPRIGHT), c);
		
		c.gridx = 0;
		c.gridy = 1;
		this.add(new RelativePosition(Position.LEFT), c);
		
		c.gridx = 2;
		c.gridy = 1;
		this.add(new RelativePosition(Position.RIGHT), c);
		
		c.gridx = 0;
		c.gridy = 2;
		this.add(new RelativePosition(Position.BOTTOMLEFT), c);
		
		c.gridx = 1;
		c.gridy = 2;
		this.add(new RelativePosition(Position.BOTTOM), c);
		
		c.gridx = 2;
		c.gridy = 2;
		this.add(new RelativePosition(Position.BOTTOMRIGHT), c);
		
		this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	}
	
	public JComponent getCenteredComponent() {
		return this.centeredComponent;
	}
	
	public GridRelativePane getTopleft() {
		return topleft;
	}

	public GridRelativePane getTop() {
		return top;
	}

	public GridRelativePane getTopright() {
		return topright;
	}

	public GridRelativePane getLeft() {
		return left;
	}

	public GridRelativePane getRight() {
		return right;
	}

	public GridRelativePane getBottomleft() {
		return bottomleft;
	}

	public GridRelativePane getBottom() {
		return bottom;
	}

	public GridRelativePane getBottomright() {
		return bottomright;
	}

	public boolean hasUniqueComponent() {
		return this.uniqueComponent;
	}
	
	public void addOnTopLeft(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.topleft = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnTop(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.top = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnTopRight(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 0;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.topright = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnLeft(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.left = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnRight(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 1;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.right = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottomLeft(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.bottomleft = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottom(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 2;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.bottom = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottomRight(JComponent component) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 2;
		
		GridRelativePane grp = this.addComponent(component, c);
		this.bottomright = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnTopLeft(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		
		this.add(grp, c);
		this.topleft = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnTop(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 0;
		
		this.add(grp, c);
		this.top = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnTopRight(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 0;
		
		this.add(grp, c);
		this.topright = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnLeft(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		
		this.add(grp, c);
		this.left = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnRight(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 1;
		
		this.add(grp, c);
		this.right = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottomLeft(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		
		this.add(grp, c);
		this.bottomleft = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottom(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 2;
		
		this.add(grp, c);
		this.bottom = grp;
		
		this.uniqueComponent = false;
	}
	
	public void addOnBottomRight(GridRelativePane grp) {
		//TODO
		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 2;
		
		this.add(grp, c);
		this.bottomright = grp;
		
		this.uniqueComponent = false;
	}
	
	private GridRelativePane addComponent(JComponent jc, GridBagConstraints c) {
		GridRelativePane grp = new GridRelativePane(jc);
		this.add(grp, c);
		return grp;
	}
	
	public void drying(Boolean state) {
		if(state) {
			this.setBorder(null);
		} else {
			this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		}
		for(Component c: this.getComponents()) {
			if(c.getClass().isAssignableFrom(RelativePosition.class)) {
				RelativePosition rp = (RelativePosition) c;
				rp.setVisible(!state);
			}
			
			if(c.getClass().isAssignableFrom(GridRelativePane.class)) {
				GridRelativePane grp = (GridRelativePane) c;
				grp.drying(state);
			}
		}
	}
	
	public String toString() {
		return "the relative grid pane";
	}
}
